package org.openscheduler.modules.scheduler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import org.openscheduler.common.util.PropertiesUtils;
import org.openscheduler.modules.job.JobService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SchedulerService {
	
	protected Logger logger = LoggerFactory.getLogger(SchedulerService.class);
	
	@Autowired
	JobService jobService;
	
	@Autowired
	SchedulerRepository schedulerRepository;
	
	public List<SchedulerMetaDataWrapper> getAllSchedulerMetaDataWrappers() throws SchedulerException {
		List<SchedulerMetaDataWrapper> metaDatas = new ArrayList<SchedulerMetaDataWrapper>();
		Collection<Scheduler> schedulers = new StdSchedulerFactory().getAllSchedulers();
		for (Scheduler scheduler : schedulers) {
			metaDatas.add(new SchedulerMetaDataWrapper(scheduler.getMetaData()));
		}
		return metaDatas;
	}
	
	public SchedulerWrapper getSchedulerWrapper(String schedName) throws SchedulerException {
		Scheduler scheduler = getScheduler(schedName);
		SchedulerWrapper wrapper = new SchedulerWrapper(scheduler);
		wrapper.setJobDetails(jobService.getAllJobDetails(scheduler));
		return wrapper;
	}
	
	public void pauseAll(String schedName) throws SchedulerException {
		logger.info("Scheduler : [{}] Pause all triggers", schedName);
		getScheduler(schedName).pauseAll();
	}
	
	public void resumeAll(String schedName) throws SchedulerException {
		logger.info("Scheduler : [{}] Resume (un-pause) all triggers", schedName);
		getScheduler(schedName).resumeAll();
	}
	
	public Collection<Scheduler> getAllSchedulers() throws SchedulerException {
		return new StdSchedulerFactory().getAllSchedulers();
	}
	
	public Scheduler getScheduler(String schedName) throws SchedulerException {
		return new StdSchedulerFactory().getScheduler(schedName);
	}
	
	public boolean checkExists(String schedName) throws SchedulerException {
		return getScheduler(schedName) != null;
	}

	public String createScheduler(String props) throws SchedulerException, IOException {
		Properties properties = PropertiesUtils.fromText(props);
		
		boolean isClustered = "true".equalsIgnoreCase(properties.getProperty("org.quartz.jobStore.isClustered", "false"));
		String schedName = properties.getProperty("org.quartz.scheduler.instanceName", StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME);
		if (!isClustered && checkExists(schedName)) {
			throw new RuntimeException("A non-clustered scheduler name " + schedName + " already exists.");
		}
		schedulerRepository.create(props);
		
		StdSchedulerFactory factory = new StdSchedulerFactory(props);
		return factory.getScheduler().getSchedulerName();
	}
	
}
